/**
 *
 */
package com.hpxx.ylzswl.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.hpxx.ylzswl.MainActivity;
import com.hpxx.ylzswl.R;
import com.hpxx.ylzswl.base.BaseActivity;
import com.hpxx.ylzswl.https.HttpAddress;
import com.hpxx.ylzswl.https.RequestParams;
import com.hpxx.ylzswl.info.ManageLoginInfo;
import com.hpxx.ylzswl.utils.ConstantsUtils;
import com.universal.frame.generalutils.Constants;
import com.universal.frame.generalutils.JsonUtil;
import com.universal.frame.generalutils.SharedPreferencesUtil;
import com.universal.frame.generalutils.ToastUtil;
import com.universal.frame.okhttp.OKHttpUtils;
import com.universal.frame.okhttp.ResultCallBack;

import java.io.IOException;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;
import okhttp3.Request;

/**
 * 云码科技.All rights reserved
 *
 * @author LuoXiang
 * @ClassName LoginActivity
 * @TitleName 登录界面
 * @Data 2016-9-9 上午9:27:52
 */
public class LoginActivity extends BaseActivity implements ResultCallBack, View.OnClickListener {

    private EditText mEditTextPhone; //电话
    private EditText mEditTextPwd; //密码
    private TextView btn_login;
    private TextView tv_login_dg_pwd;

    private String phone;
    private String pwd;
    //记录用户首次点击返回键的时间
    private long firstTime = 0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        setSwipeBackEnable(false);
    }

    @Override
    public void initView() {
        mEditTextPhone = (EditText) findViewById(R.id.et_login_phone);
        mEditTextPwd = (EditText) findViewById(R.id.et_login_pwd);
        btn_login = (TextView) findViewById(R.id.btn_login);

        tv_login_dg_pwd = (TextView) findViewById(R.id.tv_login_dg_pwd);

        btn_login.setOnClickListener(this);

        tv_login_dg_pwd.setOnClickListener(this);

//        mEditTextPhone.setText(SharedPreferencesUtil.getString(this, ConstantsUtils.USER_PHONE, ""));
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.tv_login_dg_pwd: //忘记密码
                Intent intent = new Intent(this, FindPwdActivity.class);
                startActivity(intent);
                break;

            case R.id.btn_login: //登录
                loginPost();
                Log.i("tag", "登录");
                break;


            default:
                break;
        }
    }

    public void loginPost() {

        phone = mEditTextPhone.getText().toString().trim();
        pwd = mEditTextPwd.getText().toString().trim();

        if (TextUtils.isEmpty(phone)) {
            ToastUtil.showToast(this, "请输入手机号码");
            return;
        }

        if (!phone.matches(Constants.PHONE_ZZ)) {
            ToastUtil.showToast(this, "手机号格式不正确");
            return;
        }

        if (TextUtils.isEmpty(pwd)) {
            ToastUtil.showToast(this, "请输入密码");
            return;
        }


        OKHttpUtils.postAsync(this
                , HttpAddress.SERVICE_LOGIN_URL
                , new RequestParams(this).getLoginParams(phone, pwd,3)
                , this
                , R.string.hint_text_login_loading_data, 1);
    }

    @Override
    public void onSuccess(String response, int flag) {
        Log.i("loginTAG", "response = " + response);
        String status = JsonUtil.getString(response, "status", "");
        if ("1".equals(status)) {
            String data = JsonUtil.getString(response, "data", "");
            ManageLoginInfo info = JSON.parseObject(data, ManageLoginInfo.class);
            //用户Id
            SharedPreferencesUtil.putString(this, ConstantsUtils.USER_ID, info.logistic.logisticId);
            //用户姓名
            SharedPreferencesUtil.putString(this, ConstantsUtils.USER_NAME, info.logistic.logisticRealname);
//            //用户密码
//            SharedPreferencesUtil.putString(this, ConstantsUtils.USER_PSW, info.logistic.logisticPwd);
            //用户性别
            SharedPreferencesUtil.putString(this, ConstantsUtils.USER_SEX, info.logistic.logisticSex);
            //电话
            SharedPreferencesUtil.putString(this, ConstantsUtils.USER_PHONE, info.logistic.logisticTel);
            //用户生日
            SharedPreferencesUtil.putString(this, ConstantsUtils.USER_BTD, info.logistic.logisticBirthday);
            //服务电话
            SharedPreferencesUtil.putString(this, ConstantsUtils.USER_SERVERTEL, info.serviceTel);
            SharedPreferencesUtil.putBoolean(this, ConstantsUtils.IS_LOGIN, true);
            String userId =   SharedPreferencesUtil.getString(this, ConstantsUtils.USER_ID);
            JPushInterface.setAlias(getApplicationContext(), //上下文对象
                    userId, //别名
                    new TagAliasCallback() {//回调接口,i=0表示成功,其它设置失败
                        @Override
                        public void gotResult(int i, String s, Set<String> set) {
                            Log.e("lzxxxxxxxxxxxxxxxxx", "gotResult: "+i+ s+set);
                            SharedPreferencesUtil.putBoolean(LoginActivity.this, ConstantsUtils.IS_ALIAS, true);
                        }
                    });

            ToastUtil.showToast(this, "登录成功");
            startActivity(new Intent(this, MainActivity.class));
            this.finish();
        } else {
            String msg = JsonUtil.getString(response, "msg", "");
            ToastUtil.showToast(this, msg);
        }

    }

    @Override
    public void onFailure(Request request, IOException e, int flag) {
        // TODO Auto-generated method stub
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if (System.currentTimeMillis() - firstTime > 2000) {
                Toast.makeText(LoginActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                firstTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
